package com.android.xunxian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.xunxian.R;
import com.android.xunxian.entry.DiviceDeilst;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by 设备指标 on 2016/7/3.
 */
public class DeviceDeilstAdpter extends BaseAdapter {

    private Context context;
    private List<DiviceDeilst> deviceList;

    public DeviceDeilstAdpter(Context context, List<DiviceDeilst> deviceList) {
        this.context = context;
        this.deviceList = deviceList;
    }


    @Override
    public int getCount() {
        return deviceList.size();
    }

    @Override
    public Object getItem(int position) {
        return deviceList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.deilst_device_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.zbz.setText(deviceList.get(position).getCURQ());
        viewHolder.dw.setText(deviceList.get(position).getDW());
        viewHolder.bt.setText(deviceList.get(position).getVARNAME());
        viewHolder.time.setText("最后采集时间："+deviceList.get(position).getLASTDT());

        if (deviceList.get(position).getZT() != null ) {
            int b=Integer.parseInt(deviceList.get(position).getZT());
            if(b==1){
                viewHolder.jkState.setImageResource(R.mipmap.zc_img);
            }else  if(b>1){
                viewHolder.jkState.setImageResource(R.mipmap.error_img);
            } else{
                viewHolder.jkState.setImageResource(R.mipmap.jg_img);
            }
        } else {
            viewHolder.jkState.setImageResource(R.mipmap.error_img);
        }
        if (deviceList.get(position).getWARNMARK()!= null ) {
            int b=Integer.parseInt(deviceList.get(position).getWARNMARK());
            if(b==1){
                viewHolder.bjState.setImageResource(R.mipmap.zc_img);
            }else  if(b>1){
                viewHolder.bjState.setImageResource(R.mipmap.error_img);
            } else{
                viewHolder.bjState.setImageResource(R.mipmap.jg_img);
            }

        } else {
            viewHolder.bjState.setImageResource(R.mipmap.error_img);
        }


        return convertView;
    }




    class ViewHolder {
        @InjectView(R.id.zbz)
        TextView zbz;
        @InjectView(R.id.bt)
        TextView bt;
        @InjectView(R.id.time)
        TextView time;
        @InjectView(R.id.dw)
        TextView dw;
        @InjectView(R.id.jkState)
        ImageView jkState;
        @InjectView(R.id.bjState)
        ImageView bjState;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
